<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * listmembers.php
 * begin: Wed December 18 2002
 * 
 * $Id: listmembers.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/
 
define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

// Start session management
$user->start();
$user->setup();
$user->check_auth('u_member_list');

$sort_order = array(
    0 => array('member_name', 'member_name desc'),
    1 => array('member_earned desc', 'member_earned'),
    2 => array('member_spent desc', 'member_spent'),
    3 => array('member_adjustment desc', 'member_adjustment'),
    4 => array('member_current desc', 'member_current'),
    5 => array('member_lastraid desc', 'member_lastraid'),
    6 => array('member_level desc', 'member_level'),
    7 => array('member_class', 'member_class desc')
);

$current_order = switch_order($sort_order);

//
// Compare members
//
if ( isset($_POST['submit']) && ($_POST['submit'] == $user->lang['compare_members']) && isset($_POST['compare_ids']) )
{
    // To compare members, we take the post checkboxes,
    // serialize them and pass them to the script again through _GET
    $compare_ids = serialize($_POST['compare_ids']);
    $compare_ids = urlencode($compare_ids);

    redirect('listmembers.php?compare='.$compare_ids);
}
elseif ( isset($_GET['compare']) )
{
    $s_compare = true;
    
    $compare_ids = urldecode(stripslashes($_GET['compare']));
    $compare_ids = unserialize($compare_ids);
    
    // Find 30 days ago, then find how many raids occurred in those 30 days
    // Do the same for 60 and 90 days
    $thirty_days = mktime(0, 0, 0, date('m'), date('d')-30, date('Y'));
    $ninety_days = mktime(0, 0, 0, date('m'), date('d')-90, date('Y'));
    
    $raid_count_30 = $db->query_first('SELECT count(*) FROM ' . RAIDS_TABLE . ' WHERE raid_date BETWEEN '.$thirty_days.' AND '.time());
    $raid_count_90 = $db->query_first('SELECT count(*) FROM ' . RAIDS_TABLE . ' WHERE raid_date BETWEEN '.$ninety_days.' AND '.time());
    
    $sql = 'SELECT *, (member_earned-member_spent+member_adjustment) AS member_current 
            FROM ' . MEMBERS_TABLE . '
            WHERE member_id=';
    
    foreach($compare_ids as $k => $v)
    {
        // Add the id onto the sql query
        $this_sql = $sql . $v;
        
        if ( !($member_result = $db->query($this_sql)) )
        {
            message_die('Could not obtain member information', '', __FILE__, __LINE__, $this_sql);
        }
        $row = $db->fetch_record($member_result);
        
        $individual_raid_count_30 = 0;
        $individual_raid_count_90 = 0;
        
        $rc_sql = 'SELECT count(*)
                   FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
                   WHERE (ra.raid_id = r.raid_id)
                   AND (ra.member_name='".$row['member_name']."')
                   AND (r.raid_date BETWEEN ".$thirty_days.' AND '.time().')';
        $individual_raid_count_30 = $db->query_first($rc_sql);
        
        $rc_sql = 'SELECT count(*)
                   FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
                   WHERE (ra.raid_id = r.raid_id)
                   AND (ra.member_name='".$row['member_name']."')
                   AND (r.raid_date BETWEEN ".$ninety_days.' AND '.time().')';
        $individual_raid_count_90 = $db->query_first($rc_sql);
        
        // Prevent division by 0
        $percent_of_raids_30 = ( $raid_count_30 > 0 ) ? round(($individual_raid_count_30 / $raid_count_30) * 100) : 0;
        $percent_of_raids_90 = ( $raid_count_90 > 0 ) ? round(($individual_raid_count_90 / $raid_count_90) * 100) : 0;
        
        // If the member's spent is greater than 0, see how long ago they looted an item
        
        if ( $row['member_spent'] > 0 )
        {
            $ll_sql = 'SELECT max(item_date) AS last_loot
                       FROM ' . ITEMS_TABLE . "
                       WHERE item_buyer='".$row['member_name']."'";
            $last_loot = $db->query_first($ll_sql);
        }
        
        $tpl->assign_block_vars('members_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'ID' => $row['member_id'],
            'NAME' => $row['member_name'],
            'LEVEL' => ( $row['member_level'] > 0 ) ? $row['member_level'] : '&nbsp;',
            'CLASS' => ( !empty($row['member_class']) ) ? $row['member_class'] : '&nbsp;',
            'EARNED' => $row['member_earned'],
            'SPENT' => $row['member_spent'],
            'ADJUSTMENT' => $row['member_adjustment'],
            'CURRENT' => $row['member_current'],
            'LASTRAID' => ( !empty($row['member_lastraid']) ) ? date($user->style['date_notime_short'], $row['member_lastraid']) : '&nbsp;',
            'LASTLOOT' => ( isset($last_loot) ) ? date($user->style['date_notime_short'], $last_loot) : '&nbsp;',
            'RAIDS_30_DAYS' => sprintf($user->lang['of_raids'], $percent_of_raids_30),
            'RAIDS_90_DAYS' => sprintf($user->lang['of_raids'], $percent_of_raids_90),
            'C_ADJUSTMENT' => color_item($row['member_adjustment']),
            'C_CURRENT' => color_item($row['member_current']),
            'C_LASTRAID' => 'neutral',
            'C_RAIDS_30_DAYS' => color_item($percent_of_raids_30, true),
            'C_RAIDS_90_DAYS' => color_item($percent_of_raids_90, true),
            'U_VIEW_MEMBER' => 'viewmember.php'.$SID . '&amp;' . URI_NAME . '='.$row['member_name'])
        );
        unset($last_loot);
        $db->free_result($member_result);
    }
    $footcount_text = $user->lang['listmembers_compare_footcount'];
}
//
// Normal member display
//
else
{
    $s_compare = false;

    $member_count = 0;
    $previous_data = '';
    
    // Figure out what data we're comparing from member to member
    // in order to rank them
    $sort_index = explode('.', $current_order['uri']['current']);
    $previous_source = preg_replace('/( (asc|desc))?/i', '', $sort_order[$sort_index[0]][$sort_index[1]]);
    
    $show_all = ( (!empty($_GET['show'])) && ($_GET['show'] == 'all') ) ? true : false;
    $find_class = ( !empty($_GET['class']) ) ? $_GET['class'] : false;
    
    $where = false;
    $sql = 'SELECT *, (member_earned-member_spent+member_adjustment) AS member_current 
            FROM ' . MEMBERS_TABLE;
    if ( ($eqdkp->config['hide_inactive'] == 1) && (!$show_all) )
    {
        $where = true;
        $sql .= " WHERE member_status='1'";
    }
    $sql .= ( $find_class ) ? (( $where ) ? ' AND' : ' WHERE') . " member_class='".$find_class."'" : '';
    $sql .= ' ORDER BY '.$current_order['sql'];
    
    if ( !($members_result = $db->query($sql)) )
    {
        message_die('Could not obtain member information', '', __FILE__, __LINE__, $sql);
    }
    while ( $row = $db->fetch_record($members_result) )
    {
        $member_count++;
        $tpl->assign_block_vars('members_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'ID' => $row['member_id'],
            'COUNT' => ($row[$previous_source] == $previous_data) ? '&nbsp;' : $member_count,
            'NAME' => $row['member_name'],
            'LEVEL' => ( $row['member_level'] > 0 ) ? $row['member_level'] : '&nbsp;',
            'CLASS' => ( !empty($row['member_class']) ) ? $row['member_class'] : '&nbsp;',
            'EARNED' => $row['member_earned'],
            'SPENT' => $row['member_spent'],
            'ADJUSTMENT' => $row['member_adjustment'],
            'CURRENT' => $row['member_current'],
            'LASTRAID' => ( !empty($row['member_lastraid']) ) ? date($user->style['date_notime_short'], $row['member_lastraid']) : '&nbsp;',
            'C_ADJUSTMENT' => color_item($row['member_adjustment']),
            'C_CURRENT' => color_item($row['member_current']),
            'C_LASTRAID' => 'neutral',
            'U_VIEW_MEMBER' => 'viewmember.php' . $SID . '&amp;' . URI_NAME . '='.$row['member_name'])
        );
        
        unset($last_loot);
        
        // So that we can compare this member to the next member,
        // set the value of the previous data to the source
        $previous_data = $row[$previous_source];
    }
    
    if ( ($eqdkp->config['hide_inactive'] == 1) && (!$show_all) )
    {
        $footcount_text = sprintf($user->lang['listmembers_active_footcount'], $db->num_rows($members_result), 
                                  '<a href="listmembers.php'.$SID.'&amp;o='.$current_order['uri']['current'].'&amp;show=all" class="rowfoot">');
    }
    else
    {
        $footcount_text = sprintf($user->lang['listmembers_footcount'], $db->num_rows($members_result));
    }
    $db->free_result($members_result);
}

$tpl->assign_vars(array(
    'F_MEMBERS' => 'listmembers.php'.$SID,
    
    'L_NAME' => $user->lang['name'],
    'L_LEVEL' => $user->lang['level'],
    'L_CLASS' => $user->lang['class'],
    'L_EARNED' => $user->lang['earned'],
    'L_SPENT' => $user->lang['spent'],
    'L_ADJUSTMENT' => $user->lang['adjustment'],
    'L_CURRENT' => $user->lang['current'],
    'L_LASTRAID' => $user->lang['lastraid'],
    'L_LASTLOOT' => $user->lang['lastloot'],
    'L_RAIDS_30_DAYS' => sprintf($user->lang['raids_x_days'], 30),
    'L_RAIDS_90_DAYS' => sprintf($user->lang['raids_x_days'], 90),
    'BUTTON_NAME' => 'submit',
    'BUTTON_VALUE' => $user->lang['compare_members'],
    
    'O_NAME' => $current_order['uri'][0],
    'O_LEVEL' => $current_order['uri'][6],
    'O_CLASS' => $current_order['uri'][7],
    'O_EARNED' => $current_order['uri'][1],
    'O_SPENT' => $current_order['uri'][2],
    'O_ADJUSTMENT' => $current_order['uri'][3],
    'O_CURRENT' => $current_order['uri'][4],
    'O_LASTRAID' => $current_order['uri'][5],
    
    'U_LIST_MEMBERS' => 'listmembers.php'.$SID.'&amp;',
    
    'S_COMPARE' => $s_compare,
    'S_TRANSFER' => false,
    'SHOW' => ( isset($_GET['show']) ) ? $_GET['show'] : '',
    
    'LISTMEMBERS_FOOTCOUNT' => ( isset($_GET['compare']) ) ? sprintf($footcount_text, sizeof($compare_ids)) : $footcount_text)
);

$page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listmembers_title'];
include_once($eqdkp_root_path . 'includes/page_header.php');

$tpl->set_filenames(array(
    'body' => 'listmembers.html')
);

include_once($eqdkp_root_path . 'includes/page_tail.php');
?>